package com.siboxuetang.need.dto;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ConverterUtils {

    private ConverterUtils(){}

    /**
     * 将dto中的orm取出，一般情况下请勿进行此操作，dto为封装orm的javabean，能够防止隐私信息收到修改，convertible将orm对象封装在dto中。
     * dto中包含所有可安全进行交换的信息。
     * @param <T> Orm
     * @param dto data transfer object
     * @param type 取出对象信息
     * @return orm.*
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static <T> T getJavaBean(Convertible<T> dto, Class<T> type) {
        if(dto ==null) {
            try {
                return type.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        Class clazz = dto.getClass();
        Field[] fields = clazz.getDeclaredFields();
        T bean = null;
        for (Field field : fields) {
            if (field.getType().getName().equals(type.getName())) {
                field.setAccessible(true);
                try {
                    bean = (T) field.get(dto);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return bean;
    }


    public static <E,V> E getDto(V orm,Class<E> clazz){
        Convertible<V> convertible = null;
        if(orm == null) {
            return null;
        }
        try {
            Object instance = clazz.newInstance();
            if (instance instanceof Convertible) {
                convertible = (Convertible<V>) clazz.newInstance();
            }
            setJavaBean2Dto(convertible, orm);
            return (E) convertible;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return (E) convertible;
    }

    public static <T,E> List<E> getJavaBeanList(List<T> orms,Class<E> clazz) {
        List<E> dtoList = new ArrayList<>();
        for (int i = 0; i < orms.size(); i++) {
            T orm = orms.get(i);
            Convertible<T> c = null;
            try {
                E dto =  clazz.newInstance();
                if(dto instanceof Convertible) {
                    c = (Convertible<T>) dto;
                }
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
            ConverterUtils.setJavaBean2Dto(c, orm);
            dtoList.add((E) c);
        }
        return dtoList;
    }

    /**
     * 在聚合层，将orm封入dto中
     * @param dto 数据传输对象
     * @param orm 数据映射对象
     * @param <T> 类型信息
     */
    private static <T> void setJavaBean2Dto(Convertible<T> dto, T orm) {
        if(dto == null || orm == null) {
            return;
        }
        Class clazz = dto.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.getType().getName().equals(orm.getClass().getName())) {
                field.setAccessible(true);
                try {
                    field.set(dto, orm);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
